﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseAccess;
using System.Data;
namespace Liguo.Administration
{
    public partial class OrderDeatailInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String orderId = Request.QueryString["id"].ToString();  //订单ID号 
            Session["orderid"] = orderId;
            //SqlIt sql = new SqlIt();
            if (!IsPostBack)
            {
                redraw();
            }

        }

        public void redraw()
        {
            SqlIt sql = new SqlIt();
            String orderId = (String)Session["orderid"];
            //第一步：根据订单ID，先查订单基本信息表，用户表，订单详情表    
            //把用户名、订单ID、下单时间、订单状态、备注显示             
            String field = "C.UserName,B.ID,B.[Time],D.Status,B.Remark";
            String tablename = "OrderInfo B,UserInfo C,OrderStatus D";
            String condition = " B.ID=" + orderId + " and B.UserID=C.ID and B.Status=D.ID";
            DataSet dataset = sql.SpecialItem(tablename, condition, field);
            //下面是第一步查询内容的显示
            TextBox_UserName.Text = dataset.Tables[0].Rows[0][0].ToString();
            TextBox_OrderId.Text = dataset.Tables[0].Rows[0][1].ToString();
            TextBox_Time.Text = dataset.Tables[0].Rows[0][2].ToString();
            TextBox_OrderStatus.Text = dataset.Tables[0].Rows[0][3].ToString();
            TextBox_Remark.Text = dataset.Tables[0].Rows[0][4].ToString();

            //要用表 ：订单详细信息 ，订单商品，计算方式的表  --多个
            //第二步：一个订单ID对应多个订单商品 
            //:::::::每个订单商品对应一个   商品信息
            field = "B.PicturePath,B.GoodsName,C.Method,A.Amount,A.Weight,B.Price as GoodsPerPrice,B.SaleMethod,A.Price as OrderSumPrice";
            tablename = "OrderDetail A,GoodsInfo B,SaleMethod C";
            condition = "A.GoodsID=B.ID and B.SaleMethod=C.ID and A.OrderID=" + orderId;
            DataSet goodsds = sql.SpecialItem(tablename, condition, field);
            
            //绑定数据之前对Dataset进行预先处理   进行判断amount 和weight是否为 null的处理
            for (int i = 0; i < goodsds.Tables[0].Rows.Count; i++)
            {
                String amount = goodsds.Tables[0].Rows[i]["Amount"].ToString();
                String weight=goodsds.Tables[0].Rows[i]["Weight"].ToString();
                String method=goodsds.Tables[0].Rows[i]["Method"].ToString();
                method=method.Replace(" ","");
                if (amount == null || amount.Equals(""))
                {
                    goodsds.Tables[0].Rows[i]["Amount"] = "0";
                    amount = "0";
                }
                if (weight == null || weight.Equals(""))
                {
                    goodsds.Tables[0].Rows[i]["Weight"] = "0";
                    weight = "0";
                }
                double amountval = Convert.ToDouble(amount);
                double weightval = Convert.ToDouble(weight);
                double perprice=Convert.ToDouble(goodsds.Tables[0].Rows[i]["GoodsPerPrice"].ToString());
                //下面是根据对应的method来计算总价
                if (method == "整卖整算" || method == "散卖整算")
                {
                    goodsds.Tables[0].Rows[i]["OrderSumPrice"] = amountval * perprice;
                }
                else
                {
                    goodsds.Tables[0].Rows[i]["OrderSumPrice"] = weightval * perprice;
                }                
            }
            DataList_GoodsInfo.DataSource = goodsds.Tables[0].DefaultView;
            DataList_GoodsInfo.DataBind();
        }
        protected void itemModifyEvent(object source, DataListCommandEventArgs e)
        {
            //int i = 1;
            if (e.CommandName == "modify")
            {
                 SqlIt sql = new SqlIt();
                 Label Lbl_GoodsName = e.Item.FindControl("Label_GoodsName") as Label;
                 String goodname = Lbl_GoodsName.Text;
                 String orderid = (String)Session["orderid"];      //获取订单的ID
              
                 String condition = "GoodsName='" + goodname + "'";//下面要查出货物的ID
                 String goodid = (String)sql.SingleSpecialItem("GoodsInfo", condition, "ID").ToString();

                //下面是要更新数据库库中每一条记录中amount weight
                //先要获取对象:
                Label Lbl_PerPrice = e.Item.FindControl("Label_PerPrice") as Label;
                Label Lbl_CalType = e.Item.FindControl("Label_Type") as Label;
                TextBox Txt_Amount = e.Item.FindControl("TextBox_Amount") as TextBox;
                TextBox Txt_Weight = e.Item.FindControl("TextBox_Weight") as TextBox;
                Label Lbl_AllCost = e.Item.FindControl("Label_AllCost") as Label;
 
                String amount = Txt_Amount.Text;
                String weight = Txt_Weight.Text;
                String change = "Weight='" + weight + "',Amount='"+amount+"'";
                condition = "GoodsID='" + goodid + "' AND OrderID="+orderid;
                sql.SpecialUpdate("OrderDetail",change,condition);
                redraw();
            }
            else if (e.CommandName == "delete")
            {
                //后面代码从订单详情中进行删除操作       但是先不考虑全部删完后要删除简略信息表中记录的操作
                SqlIt sql = new SqlIt();
                Label Lbl_GoodsName = e.Item.FindControl("Label_GoodsName") as Label;
                String goodname = Lbl_GoodsName.Text;
                String orderid = (String)Session["orderid"];
                //先查出goodid
                String condition = "GoodsName='" + goodname + "'";
                String goodid = (String)sql.SingleSpecialItem("GoodsInfo", condition, "ID").ToString();
                condition = "GoodsID=" + goodid + " AND OrderID=" + orderid;
                sql.SpecialDelete("OrderDetail", condition);
                redraw();
                //Response.Redirect("OrderDetailInfo.aspx?id="+orderid);
            }
        }


    }
}